package com.hxstrive.freemarker.demo1;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;

import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class Demo1 {

    public static void main(String[] args) throws Exception {
        // 1. Configure FreeMarker
        // You should do this ONLY ONCE, when your application starts,
        // then reuse the same Configuration object elsewhere.
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        // Where do we load the templates from:
        cfg.setClassForTemplateLoading(Demo1.class, "/templates/");
        // Some other recommended settings:
        cfg.setDefaultEncoding("UTF-8");
        cfg.setLocale(Locale.CHINESE);
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // 2. Proccess template(s)
        // You will do this for several times in typical applications.
        // 2.1. Prepare the template input:
        Map<String, Object> input = new HashMap<String, Object>();
        input.put("username", "www.hxstrive.com");

        // 2.2. Get the template
        Template template = cfg.getTemplate("template.ftl");

        // 2.3. Generate the output

        // Write output to the console
        Writer consoleWriter = new OutputStreamWriter(System.out);
        template.process(input, consoleWriter);

        // For the sake of example, also write output into a file:
        Writer fileWriter = new FileWriter(new File("output.txt"));
        try {
            template.process(input, fileWriter);
        } finally {
            fileWriter.close();
        }
    }

}
